//
//  JWNewsController.m
//  见闻新闻
//
//  Created by brother on 16/3/11.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWNewsController.h"
#import "JWCategoryModel.h"
#import "JWNewsModel.h"
#import "JWNewsWebController.h"
#import "JWNewsCell.h"
@interface JWNewsController ()<MBProgressHUDDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,copy) NSString *themes_id;

@property(nonatomic,retain)NSMutableArray *newsArray;

@property (nonatomic ,strong)JWNewsModel *newsModel;

@property (nonatomic ,assign)BOOL isRefresh;

@property (nonatomic ,copy)NSString *max_id;

@property (weak, nonatomic) IBOutlet UITableView *myTabelView;

@property (nonatomic ,copy)NSString *themes;

@end

@implementation JWNewsController

-(NSMutableArray *)newsArray {
    
    if (_newsArray == nil) {
        _newsArray = [NSMutableArray array];
    }
    return _newsArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(self.categoryBLock){
        JWCategoryModel *category = self.categoryBLock();
        
        self.themes = category.themes_id;
 
        self.navigationItem.title = category.title;
    }

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(backBarClick)];
    
    [backItem setImage:[UIImage imageNamed:@"tabbar_home"]];
    
    self.navigationItem.leftBarButtonItem = backItem;
    
    [self testfoot];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self requestAFN];
}

- (void)backBarClick {
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)requestAFN {
    NSLog(@"55");
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    hud.labelText = @"正在加载";
    
    hud.delegate = self;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    NSString *str = @"http://1.zhixinnews.applinzi.com/Home/Zhixinnews/querynews";
    NSDictionary *dic = @{@"themes_id":self.themes};
    [manager GET:str parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableArray *dataArray = responseObject[@"data"];
    
        
        NSMutableArray *mutArray = [NSMutableArray array];
        
        [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            JWNewsModel *NewsModel = [[JWNewsModel alloc]initWithDict:obj];
            
            [mutArray addObject:NewsModel];
        }];
        self.newsArray = mutArray;
        
        dispatch_async(dispatch_get_main_queue(), ^{

            [hud hide:YES];
        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hide:YES];
        });
        NSLog(@"%@",error);
    }];
    
}

- (void)hudWasHidden:(MBProgressHUD *)hud {
    [self.myTabelView reloadData];

}

//http://1.zhixinnews.applinzi.com/Home/Zhixinnews/showFound
- (void)testfoot {
    self.myTabelView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.newsArray.count > 0) {
            JWNewsModel *news = [self.newsArray lastObject];
            self.max_id = news.newsId;
            NSLog(@"%@",self.max_id);
        }else {
            //self.feature = [self.newsMutArray lastObject];
            self.max_id = @"100";
        }
        [self shanglaRefreshAFFFFFF:self.max_id];
        self.isRefresh = YES;
        [self.myTabelView.mj_footer endRefreshing];
    }];
}
- (void)shanglaRefreshAFFFFFF:(NSString *)max_id{
    if (self.isRefresh == YES) {
        return;
        
    }else {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"正在加载";
        NSString *str = @"http://1.zhixinnews.applinzi.com/Home/Zhixinnews/showFound";
        NSDictionary *dic = @{@"max_id":self.max_id};
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager GET:str parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSArray *dataArray = responseObject[@"data"];
            
            NSMutableArray *mutArray = [NSMutableArray array];
            
            //            if ([self.themes_id isEqualToString:dataArray[0][@"temes_id"]]) {
            [dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWNewsModel *news = [[JWNewsModel alloc]initWithDict:obj];
                [mutArray addObject:news];
            }];
            //            }
            NSMutableArray *tempArray = [NSMutableArray array];
            if (mutArray.count > 0) {
                [mutArray removeObjectAtIndex:0];
            }
            [tempArray addObjectsFromArray:self.newsArray];
            [tempArray addObjectsFromArray:mutArray];
            self.newsArray = tempArray;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [hud hide:YES];
                self.isRefresh = NO;
                [self.myTabelView reloadData];
            });
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [hud hide:YES];
                self.isRefresh = NO;
                [self.myTabelView reloadData];
            });
            NSLog(@"%@",error);
        }];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.newsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    JWNewsCell *cell = [JWNewsCell newsWithTableView:tableView];
    cell.news = self.newsArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    self.newsModel = self.newsArray[indexPath.row];
    [self chuazhi];
}

- (void)chuazhi {
    JWNewsWebController *newsWebControl = [[JWNewsWebController alloc]initWithNibName:@"JWNewsWebController" bundle:nil];
    newsWebControl.newsBlock = ^JWNewsModel *{
        return self.newsModel;
    };
    
    [self.navigationController pushViewController:newsWebControl animated:YES];
}

@end
